package com.example.controller;

import com.example.detail.UserDTO;
//import com.example.service.Test01Service;
import com.example.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：songxusheng
 * @date ：Created in 2022/4/8 10:18
 * @description：测试接口
 * @version: $
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);
    @Value("${server.port}")
    private String applicationServerPort;
    @Autowired
    private TestService testService;
//    @Autowired
//    private Test01Service test01Service;

    @RequestMapping("/find")
    public String find(){
        String name="张三";
//        String call01 = test01Service.call(name);
//        String call01 = "123";
        String call = testService.call(name);
        return call+"======="+call;
    }


    @PostMapping("/findAll")
    public List<UserDTO> findALl(){
        return testService.findAll();
    }

    @PostMapping("/findAllByUserDTO")
    public List<UserDTO> findAllByUserDTO(){
        UserDTO kk = new UserDTO(1, "看看", false);
        return testService.findAllByUser(kk);
    }
    @GetMapping("/goods/{goodsId}")
    public String goodsDetail(@PathVariable("goodsId") int goodsId) {
        // 模拟超时
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        // 模拟异常
         int i = 13/0;
        // 根据id查询商品并返回给调用端
        if (goodsId < 1 || goodsId > 100000) {
            return "查询商品为空，当前服务的端口号为" + applicationServerPort;
        }
        String goodsName = "商品" + goodsId;
        // 返回信息给调用端
        return goodsName + "，当前服务的端口号为" + applicationServerPort;
    }

    @GetMapping("/logTest")
    public String logTest() {

        // 平时会打的日志
        log.info("test info log by sleuth");
        log.error("test error log by sleuth");

        try {
            int i = 1 / 0;
        } catch (Exception e) {
            // 将异常信息通过日志输出
            log.error("test exception log by sleuth:", e);
        }

        return "logTest";
    }
}
